package appli;

import gphoto.bo.PhotoBean;
import gphoto.services.impl.PhotoServicesImpl;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.sanselan.ImageReadException;

import algutil.temps.Chronometre;

import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.MetadataException;

public class RechercheDoublonsPhoto extends ApplicationLC {

	public static final String VERSION = "1.0.0";

	public static final String VERSION_DATE = "01/02/2010";

	private static final Logger log = Logger
			.getLogger(RechercheDoublonsPhoto.class);

	private File rep = null;

	public List<PhotoBean> photos = null;

	private boolean recursif = false;

	public RechercheDoublonsPhoto(String[] args) throws Exception {

		super();

		if (!controlArguments(args)) {
			afficherUsage();
			arretExecution(1);
		}

		Chronometre chrono = new Chronometre();
		chrono.start();
		if (!recursif) {
			process(rep);
		} else {
			processR(rep);
		}
		chrono.stop();
		log.info("Temps de traitement : " + chrono);

		arretExecution(0);
	}

	@Override
	protected void afficherUsage() {

		log.info("Usage : java appli.FormatPhotoName directory");
		log.info("    directory : repertoire a trier");
	}

	@Override
	protected boolean controlArguments(String[] args) {

		GestionArguments gargs = new GestionArguments(args, 1, 2);

		if (!gargs.hasValifNbArguments()) {
			log.error("Nombre d'argments invalide");
			log.info(gargs);
			return false;
		}

		if (!gargs.containsDirectoryPath()) {
			log.error("Pas de repertoire valide");
			log.info(gargs);
			return false;
		} else {
			rep = gargs.getFirstDirectoryPath();
		}

		if (gargs.containsArgs("-r")) {
			recursif = true;
		}

		return true;
	}

	private void processR(File dir) throws JpegProcessingException,
			MetadataException, ImageReadException, IOException {
		process(dir);
		File[] sdir = dir.listFiles();
		for (int i = 0; i < sdir.length; i++) {
			if (sdir[i].isDirectory()) {
				processR(sdir[i]);
			}
		}
	}

	private void process(File dir) throws JpegProcessingException,
			MetadataException, ImageReadException, IOException {
		log.info("%%%%%%%%% RESUME %%%%%%%%%%");
		log.info("Rep : " + dir.getPath());

		photos = PhotoServicesImpl.getInstance().recupererPhotos(dir);
		log.info("%%%%%% RECUPERATION %%%%%%%");
		log.info("Recuperation et tri de " + photos.size() + " photos");

		Collections.sort(photos);

		log.info("%%%%%%%% RECHERCHE %%%%%%%%");

		PhotoBean photo = null;
		int nbDoublons = 0;
		for (int i = 0; i < photos.size(); i++) {
			photo = photos.get(i);
			for (int j = (i + 1); j < photos.size(); j++) {
				if (i != j && photo.equals(photos.get(j))) {
					log.info("EQ : " + photo.getNom() + " <-> "
							+ photos.get(j).getNom());
					nbDoublons++;
				}
			}

		}
		log.info("Nb Doublons : " + nbDoublons);
		log.info("%%%%%%%%%%% FIN %%%%%%%%%%%");
	}

	public static void main(String[] args) throws Exception {

		new RechercheDoublonsPhoto(args);
	}

	@Override
	protected void genererCommandOptions() {
		// TODO Auto-generated method stub

	}
}
